import { Loading } from 'element-ui';

export default class VirLoading {
    private loading?: any;

    private propertySpinner?: string;
    private propertyBackground?: string;

    private propertyLock: boolean = true;
    private propertyFullscreen: boolean = true;

    set lock(v: boolean) {
        this.propertyLock = v;
    }

    set spinner(v: string) {
        this.propertySpinner = v;
    }

    set background(v: string) {
        this.propertyBackground = v;
    }

    set fullscreen(v: boolean) {
        this.propertyFullscreen = v;
    }

    /**
     * 打开加载遮盖层
     * @param text 加载提示内容
     */
    public open(text?: string): any {
        if (this.loading) return this.loading;

        return this.loading = Loading.service({
            text: text || '加载中...',
            lock: this.propertyLock,
            spinner: this.propertySpinner,
            background: this.propertyBackground,
            fullscreen: this.propertyFullscreen
        });
    }

    /**
     * 关闭加载遮盖层
     */
    public close(): void {
        if (!this.loading) return;

        this.loading.close();
        this.loading = null;
    }

    /**
     * 当前是否已经开启加载动画
     */
    public isOpen(): boolean {
        return !!this.loading;
    }
}
